import { Model } from 'sequelize';
import sequelize from '../config/db.js';
import User from './user.js';
import { DataTypes } from 'sequelize';

class SleepData extends Model {}
SleepData.init(
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    sleepType:{
      type: DataTypes.STRING,
      defaultValue: 'night',
      comment:'睡眠类型'
    },
   sleepStart: {
      type: DataTypes.DATE,
      allowNull: false,
      comment: '入睡时间'
    },
    sleepEnd: {
      type: DataTypes.DATE,
      allowNull: false,
      comment: '醒来时间'
    },
    sleepDuration: {
      type: DataTypes.DECIMAL(3, 1),
      allowNull: false,
      validate: {
        min: 0,
        max: 24
      },
      comment: '总睡眠时长，单位：小时'
    },
    lightSleepDuration: {
      type: DataTypes.DECIMAL(3, 1),
      comment: '浅睡时长，单位：小时'
    },
    deepSleepDuration: {
      type: DataTypes.DECIMAL(3, 1),
      comment: '深睡时长，单位：小时'
    },
    remSleepDuration: {
      type: DataTypes.DECIMAL(3, 1),
      comment: 'REM睡眠时长，单位：小时'
    },
    deepSleepPercentage: {
      type: DataTypes.INTEGER,
      allowNull: false,
      validate: {
        min: 0,
        max: 100
      },
      comment: '深睡比例，单位：百分比'
    },
    wakeUpTimes: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '醒来次数'
    },
    sleepQualityScore: {
      type: DataTypes.INTEGER,
      validate: {
        min: 0,
        max: 100
      },
      comment: '睡眠质量评分'
    },
    collectTime: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
    },
  },
  {
    sequelize,
    modelName: 'SleepData',
    tableName: 'sleep_data',
  }
);

SleepData.belongsTo(User, { foreignKey: 'userId', as: 'user' });
User.hasMany(SleepData, { foreignKey: 'userId', as: 'sleepData' });

export default SleepData;